package com.jgjc.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.jgjc.entity.CommonResult;
import com.jgjc.service.BasicInfoService;
import com.jgjc.test.User;
import com.jgjc.utils.ResultResUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class BasicInfoController {

    @Resource
    private BasicInfoService basicInfoService;

    @GetMapping(value = "/bridge/bridge/getDeleteStatusList")
    public List<Map<String, Object>> getDeleteStatusList() {
        List<Map<String, Object>> listMap = basicInfoService.getDeleteStatusList();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridge/getDeckDivideTypeList")
    public List<Map<String, Object>> getDeckDivideTypeList() {
        List<Map<String, Object>> listMap = basicInfoService.getDeckDivideTypeList();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridge/getCrossTypeList")
    public List<Map<String, Object>> getCrossTypeList() {
        List<Map<String, Object>> listMap = basicInfoService.getCrossTypeList();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridge/getBridgeCategoryList")
    public List<Map<String, Object>> getBridgeCategoryList() {
        List<Map<String, Object>> listMap = basicInfoService.getBridgeCategoryList();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridge/getLoadCapacityList")
    public List<Map<String, Object>> getLoadCapacityList() {
        List<Map<String, Object>> listMap = basicInfoService.getLoadCapacityList();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridge/getDesignLoadList")
    public List<Map<String, Object>> getDesignLoadList() {
        List<Map<String, Object>> listMap = basicInfoService.getDesignLoadList();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridge/getFunctionTypeList")
    public List<Map<String, Object>> getFunctionTypeList() {
        List<Map<String, Object>> listMap = basicInfoService.getFunctionTypeList();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridge/getRoadLevelList")
    public List<Map<String, Object>> getRoadLevelList() {
        List<Map<String, Object>> listMap = basicInfoService.getRoadLevelList();
        return listMap;
    }

    @GetMapping(value = "/bridge/bridge/getRoadNoList")
    public List<Map<String, Object>> getRoadNoList() {
        List<Map<String, Object>> listMap = basicInfoService.getRoadNoList();
        return listMap;
    }


    @GetMapping(value = "/bridge/bridge/getBridgePageData")
    public Map<String, Object> getBridgePageData(HttpServletRequest request) {
        int pageSize = Integer.valueOf(request.getParameter("pageSize"));
        int currentPage = Integer.valueOf(request.getParameter("currentPage"));
        String name = request.getParameter("name");
        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> bridgeMapList = basicInfoService.getAllBasicInfo(name);
        resultMap.put("currentPage", currentPage);
        resultMap.put("data", bridgeMapList);
        resultMap.put("pageSize", pageSize);
        resultMap.put("total", bridgeMapList.size());
        return resultMap;
    }

    @GetMapping(value = "/bridge/bridge/isBridgeExists")
    public boolean isBridgeExists(HttpServletRequest request) {
        String roadNoId = request.getParameter("roadNoId");
        String name = request.getParameter("name");
        String fullAddressName = request.getParameter("fullAddressName");
        Map<String, Object> isBridgeExistMap = new HashMap<>();
        isBridgeExistMap.put("roadNoId", roadNoId);
        isBridgeExistMap.put("name", name);
        isBridgeExistMap.put("fullAddressName", fullAddressName);
        List<Map<String, Object>> mapList = basicInfoService.isBridgeExists(isBridgeExistMap);
        if (mapList.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @PostMapping(value = "/bridge/bridge/add")
    public Map<String, Object> add(@RequestBody Map<Object, Object> infoMap) {
        String buildDate = (String) infoMap.get("buildDate");
        infoMap.put("buildDate", DateUtil.parse(buildDate.split("T")[0]));
        int code = basicInfoService.addBasicInfo(infoMap);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }


    @PostMapping(value = "/bridge/bridge/edit")
    public Map<String, Object> edit(@RequestBody Map<Object, Object> infoMap) {
        String buildDate = (String) infoMap.get("buildDate");
        if (buildDate != null && !"".equals(buildDate)) {
            infoMap.put("buildDate", DateUtil.parse(buildDate.split("T")[0]));
        }
        int code = basicInfoService.updateBasicInfo(infoMap);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }

    @DeleteMapping(value = "/bridge/bridge/deleteBridge")
    public Map<String, Object> deleteBridge(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        int deleteReasonId = Integer.parseInt(request.getParameter("deleteStatusId"));
        int code = basicInfoService.deleteBridge(id, deleteReasonId);
        Map<String, Object> map = new HashMap<>();
        ResultResUtils.ResultRes(code, map);
        return map;
    }

}
